package com.ruoyi.business.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.business.domain.LawCaseDetail;
import com.ruoyi.business.mapper.LawCaseMapper;
import com.ruoyi.business.domain.LawCase;
import com.ruoyi.business.service.ILawCaseService;

/**
 * caseService业务层处理
 * 
 * @author lwj
 * @date 2021-05-22
 */
@Service
public class LawCaseServiceImpl implements ILawCaseService 
{
    @Autowired
    private LawCaseMapper lawCaseMapper;

    /**
     * 查询case
     * 
     * @param caseId caseID
     * @return case
     */
    @Override
    public LawCase selectLawCaseById(Integer caseId)
    {
        return lawCaseMapper.selectLawCaseById(caseId);
    }

    /**
     * 查询case列表
     * 
     * @param lawCase case
     * @return case
     */
    @Override
    public List<LawCase> selectLawCaseList(LawCase lawCase)
    {
        return lawCaseMapper.selectLawCaseList(lawCase);
    }

    /**
     * 新增case
     * 
     * @param lawCase case
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLawCase(LawCase lawCase)
    {
        lawCase.setCreateTime(DateUtils.getNowDate());
        lawCase.setCreateBy(SecurityUtils.getUsername());
        int rows = lawCaseMapper.insertLawCase(lawCase);
        insertLawCaseDetail(lawCase);
        return rows;
    }

    /**
     * 修改case
     * 
     * @param lawCase case
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLawCase(LawCase lawCase)
    {
        lawCase.setUpdateTime(DateUtils.getNowDate());
//        lawCaseMapper.deleteLawCaseDetailByCaseId(lawCase.getCaseId());
        insertLawCaseDetail(lawCase);
        return lawCaseMapper.updateLawCase(lawCase);
    }

    /**
     * 批量删除case
     * 
     * @param caseIds 需要删除的caseID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLawCaseByIds(Integer[] caseIds)
    {
        lawCaseMapper.deleteLawCaseDetailByCaseIds(caseIds);
        return lawCaseMapper.deleteLawCaseByIds(caseIds);
    }

    /**
     * 删除case信息
     * 
     * @param caseId caseID
     * @return 结果
     */
    @Override
    public int deleteLawCaseById(Integer caseId)
    {
        lawCaseMapper.deleteLawCaseDetailByCaseId(caseId);
        return lawCaseMapper.deleteLawCaseById(caseId);
    }

    /**
     * 新增詳情表信息
     * 
     * @param lawCase case对象
     */
    public void insertLawCaseDetail(LawCase lawCase)
    {
        List<LawCaseDetail> lawCaseDetailList = lawCase.getLawCaseDetailList();
        Integer caseId = lawCase.getCaseId();
        if (StringUtils.isNotNull(lawCaseDetailList))
        {
            List<LawCaseDetail> list = new ArrayList<LawCaseDetail>();
            LawCaseDetail add = new LawCaseDetail();
//            for (LawCaseDetail lawCaseDetail : lawCaseDetailList)
//            {
                add.setCaseId(caseId);
                add.setCreateBy(lawCase.getCreateBy());
                //這邊狀態暫時寫死
                add.setCaseDetailStatus(lawCase.getCaseStatus());
                add.setCaseDetailTime(lawCase.getUpdateTime());
                list.add(add);
//            }
                lawCaseMapper.batchLawCaseDetail(list);
        }
    }
}
